<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('user_addresses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->string('name'); // 收货人姓名
            $table->string('phone'); // 手机号
            $table->string('province')->nullable(); // 省份
            $table->string('city')->nullable(); // 城市
            $table->string('district')->nullable(); // 区/县
            $table->text('address'); // 详细地址
            $table->string('postal_code')->nullable(); // 邮编
            $table->boolean('is_default')->default(false); // 是否默认地址
            $table->string('label', 50)->nullable(); // 地址标签：家、公司、学校等
            $table->timestamp('last_used_at')->nullable(); // 最后使用时间
            $table->timestamps();
            
            $table->index(['user_id', 'is_default']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('user_addresses');
    }
};
